<template>
  <el-dialog :visible.sync="visible" fullscreen lock-scroll class="JNPF-full-dialog"
    :show-close="false" :modal="false">
    <div class="JNPF-full-dialog-header">
      <div class="header-title">
        <img src="@/assets/images/Design.png" class="header-logo" />
        <p class="header-txt"> · 打印模板</p>
      </div>
      <el-steps :active="activeStep" finish-status="success" simple class="steps steps2"
        v-if="!loading">
        <el-step title="基础设置" @click.native="stepChick(0)" />
        <el-step title="打印设计" @click.native="stepChick(1)" />
      </el-steps>
      <div class="options">
        <el-button @click="prev" :disabled="activeStep<=0">{{$t('common.prev')}}</el-button>
        <el-button @click="next" :disabled="activeStep>=1 || loading" :loading="nextBtnLoading">
          {{$t('common.next')}}
        </el-button>
        <el-button type="primary" @click="dataFormSubmit()" :disabled="activeStep!=1"
          :loading="btnLoading">{{$t('common.confirmButton')}}</el-button>
        <el-button @click="closeDialog()">{{$t('common.cancelButton')}}</el-button>
      </div>
    </div>
    <div class="main" v-loading="loading">
      <el-row type="flex" justify="center" align="middle" v-if="!activeStep" class="basic-box">
        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="10" class="basicForm">
          <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px"
            @submit.native.prevent label-position="right">
            <el-form-item label="模板名称" prop="fullName">
              <el-input v-model="dataForm.fullName" placeholder="模板名称" maxlength="100">
              </el-input>
            </el-form-item>
            <el-form-item label="模板编码" prop="enCode">
              <el-input v-model="dataForm.enCode" placeholder="模板编码" maxlength="50">
              </el-input>
            </el-form-item>
            <el-form-item label="模板分类" prop="category">
              <el-select v-model="dataForm.category" placeholder="选择分类" filterable>
                <el-option :key="item.id" :label="item.fullName" :value="item.enCode"
                  v-for="item in categoryList" />
              </el-select>
            </el-form-item>
            <el-form-item label="模板类型" prop="type">
              <el-radio-group v-model="dataForm.type">
                <el-radio-button :label="1">流程表单</el-radio-button>
                <el-radio-button :label="2">功能表单</el-radio-button>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="模板排序" prop="sortCode">
              <el-input-number :min="0" :max="999999" v-model="dataForm.sortCode"
                controls-position="right" />
            </el-form-item>
            <el-form-item label="模板状态" prop="enabledMark">
              <el-switch v-model="dataForm.enabledMark" :active-value="1" :inactive-value="0" />
            </el-form-item>
            <el-form-item label="模板说明" prop="description">
              <el-input v-model="dataForm.description" placeholder="模板说明" type="textarea"
                :rows="3" />
            </el-form-item>
            <el-form-item label="数据来源" prop="sourceType">
              <el-radio-group v-model="dataForm.sourceType">
                <el-radio-button :label="0">自定义SQL</el-radio-button>
                <el-radio-button :label="1">数据接口</el-radio-button>
              </el-radio-group>
            </el-form-item>
            <template v-if="dataForm.sourceType==0">
              <div class="mb-20">
                <el-alert title="默认打印的时候必须传业务主键@formId给SQL语句里面作为Where查询条件" type="warning" show-icon
                  :closable="false"></el-alert>
              </div>
              <el-form-item label="数据连接" prop="dbLinkId">
                <el-select v-model="dataForm.dbLinkId" placeholder="请选择数据库" clearable filterable>
                  <el-option-group v-for="group in dbOptions" :key="group.fullName"
                    :label="group.fullName">
                    <el-option v-for="item in group.children" :key="item.id" :label="item.fullName"
                      :value="item.id" />
                  </el-option-group>
                </el-select>
              </el-form-item>
              <el-form-item label="SQL语句" prop="sqlTemplate">
                <el-button icon="el-icon-plus" @click="addSql()">新增SQL</el-button>
                <el-row v-for="(item,i) in sqlTemplate" :key="i" class="mt-10">
                  <el-col :span="20">
                    <el-input v-model="item.sql" placeholder="请输入SQL查询语句&存储过程语句" type="textarea"
                      :autosize="{ minRows: 3, maxRows: 10}" />
                  </el-col>
                  <el-col :span="3" :offset="1" class="delBtn">
                    <el-button type="danger" icon="el-icon-close" @click="delSql(i)">
                    </el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </template>
            <template v-else>
              <el-form-item label="数据接口" prop="interfaceId">
                <interface-dialog :value="dataForm.interfaceId" :title="dataForm.interfaceName"
                  popupTitle="数据接口" @change="onInterfaceChange" />
              </el-form-item>
              <el-table :data="parameterJson" style="margin-bottom: 18px;">
                <el-table-column type="index" width="50" label="序号" align="center" />
                <el-table-column prop="field" label="参数名称" width="200">
                  <template slot-scope="scope">
                    <span class="required-sign">{{scope.row.required?'*':''}}</span>
                    {{scope.row.fieldName?scope.row.field+'('+scope.row.fieldName+')':scope.row.field}}
                  </template>
                </el-table-column>
                <el-table-column prop="value" label="参数来源" width="150">
                  <template slot-scope="scope">
                    <el-select v-model="scope.row.sourceType" placeholder="请选择参数来源"
                      @change="handleSourceTypeChange(scope.row)">
                      <el-option v-for="item in parameterSource(scope.row.required)"
                        :key="item.value" :label="item.label" :value="item.value">
                      </el-option>
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column prop="value" label="参数值">
                  <template slot-scope="scope">
                    <template v-if="scope.row.sourceType ==2">
                      <JnpfInputNumber v-if="['int', 'decimal'].includes(scope.row.dataType)"
                        v-model="scope.row.relationField" placeholder="请输入参数值"
                        :disabled="scope.row.sourceType == 3" clearable controls
                        controlsPosition='right' class="input-number" />
                      <JnpfDatePicker v-else-if="scope.row.dataType == 'datetime'"
                        style="width:100%" v-model="scope.row.relationField" placeholder="请选择参数值"
                        format="yyyy-MM-dd HH:mm:ss" :disabled="scope.row.sourceType == 3"
                        clearable />
                      <el-input v-model="scope.row.relationField" placeholder="请输入参数值"
                        :disabled="scope.row.sourceType == 3" clearable v-else />
                    </template>
                    <el-select v-if="scope.row.sourceType ==4" v-model="scope.row.relationField"
                      placeholder="请选择系统变量" clearable filterable>
                      <el-option v-for="item in jnpf.getSystemVariables(1)" :key="item.id"
                        :label="item.fullName" :value="item.id">
                      </el-option>
                    </el-select>
                  </template>
                </el-table-column>
              </el-table>
            </template>
          </el-form>
        </el-col>
      </el-row>
      <template v-if="activeStep==1 && showPrint">
        <print-templater ref="printTemplater" :treeData="treeData" v-model="dataForm.printTemplate"
          :pageParam="pageParam" @pageParamInit="pageParamInit" @pageParamChange="pageParamChange"
          :type="dataForm.type" />
      </template>
    </div>
  </el-dialog>
</template>

<script>
import { getPrintDevInfo, Update, Create, getFields } from '@/api/system/printDev'
import { getDataSourceListAll } from '@/api/systemData/dataSource'
import PrintTemplater from './ts-print-templater'
import InterfaceDialog from '@/components/Process/PropPanel/InterfaceDialog'
export default {
  components: { PrintTemplater, InterfaceDialog },
  data() {
    return {
      visible: false,
      loading: false,
      nextBtnLoading: false,
      activeStep: 0,
      showPrint: true,
      dataForm: {
        id: '',
        fullName: '',
        enCode: '',
        dbLinkId: '0',
        type: 1,
        enabledMark: 1,
        sortCode: 0,
        category: '',
        sqlTemplate: '',
        leftFields: '',
        printTemplate: '',
        description: '',
        sourceType: 0,
        pageParam: {},
        interfaceId: ''
      },
      parameterJson: [],
      fieldList: [],
      dataRule: {
        fullName: [
          { required: true, message: '模板名称不能为空', trigger: 'blur' },
        ],
        enCode: [
          { required: true, message: '模板编码不能为空', trigger: 'blur' },
          { validator: this.formValidate('enCode'), trigger: 'blur' },
        ],
        category: [
          { required: true, message: '模板分类不能为空', trigger: 'change' },
        ],
        dbLinkId: [
          { required: true, message: '数据连接不能为空', trigger: 'change' },
        ],
        sqlTemplate: [
          { required: true, message: 'SQL语句不能为空', trigger: 'click' },
        ],
        interfaceId: [
          { required: true, message: '数据接口不能为空', trigger: 'change' },
        ]
      },
      formVisible: false,
      btnLoading: false,
      sqlTemplate: [],
      categoryList: [],
      treeData: [],
      dbOptions: [],
      pageParam: {
        mt: "10",
        mb: "10",
        ml: "10",
        mr: "10",
        type: "2",
        width: 210,
        height: 297,
        direction: "纵向"
      },
    }
  },
  methods: {
    pageParamInit(e) {
      this.pageParam = e
    },
    pageParamChange(pageParam) {
      this.dataForm.pageParam = pageParam && JSON.stringify(pageParam)
    },
    init(categoryList, id) {
      this.fieldList = []
      this.parameterJson = []
      this.categoryList = categoryList
      this.activeStep = 0
      this.dataForm.id = id || ''
      this.getDbOptions()
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.loading = true
          this.showPrint = false
          getPrintDevInfo(this.dataForm.id).then(res => {
            this.dataForm = res.data
            this.sqlTemplate = this.dataForm.sqlTemplate && JSON.parse(this.dataForm.sqlTemplate) || []
            this.parameterJson = (this.dataForm.parameterJson && JSON.parse(this.dataForm.parameterJson)) || [];
            this.fieldList = this.dataForm.fieldJson && JSON.parse(this.dataForm.fieldJson) || []
            let e = res.data.pageParam && JSON.parse(res.data.pageParam)
            this.pageParam = e
            this.pageParam.direction = "纵向"
            this.showPrint = true
            this.loading = false
          }).catch(() => { this.loading = false })
          this.showPrint = true
        }
      })
    },
    dataFormSubmit() {
      this.btnLoading = true
      // this.dataForm.sqlTemplate = JSON.stringify(this.sqlTemplate)
      const formMethod = this.dataForm.id ? Update : Create
      formMethod(this.dataForm).then((res) => {
        this.$message({
          message: res.msg,
          type: 'success',
          duration: 1500,
          onClose: () => {
            this.closeDialog(true)
          }
        })
      }).catch(() => { this.btnLoading = false })
    },
    delSql(i) {
      this.sqlTemplate.splice(i, 1)
    },
    addSql() {
      let item = { sql: "" }
      this.sqlTemplate.push(item)
    },
    exist() {
      if (!this.sqlTemplate.length) {
        this.$message.error('请输入SQL语句')
        return false
      }
      let isOk = true;
      //  遍历数组，判断非空
      for (let i = 0; i < this.sqlTemplate.length; i++) {
        const e = this.sqlTemplate[i];
        if (!e.sql) {
          this.$message({
            message: `第${i + 1}行SQL语句不能为空`,
            type: 'error',
            duration: 1000
          });
          isOk = false
          break
        }
      }
      return isOk;
    },
    next() {
      if (this.activeStep < 1) {
        if (this.dataForm.sourceType == 0 && !this.exist()) return
        this.dataForm.sqlTemplate = JSON.stringify(this.sqlTemplate)
        this.dataForm.parameterJson = JSON.stringify(this.parameterJson)
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.sourceType == 0) {
              this.nextBtnLoading = true
              const query = {
                dbLinkId: this.dataForm.dbLinkId,
                sqlTemplate: this.dataForm.sqlTemplate
              }
              getFields(query).then(res => {
                this.treeData = res.data
                this.activeStep += 1
                this.nextBtnLoading = false
              }).catch(() => {
                this.nextBtnLoading = false
              })
            }
            else {
              if (!this.fieldList.length) return this.$message.error('请先设置数据接口的字段列表！')
              this.treeData = this.fieldList.map(o => ({ ...o, fullName: o.field + '(' + o.defaultValue + ')', id: o.defaultValue }));
              this.activeStep += 1
            }
          }
        })
      }
    },
    closeDialog(isRefresh) {
      this.visible = false
      this.$emit('close', isRefresh)
    },
    prev() {
      this.activeStep -= 1
    },
    stepChick(key) {
      if (this.activeStep <= key) return
      this.activeStep = key
    },
    getDbOptions() {
      getDataSourceListAll().then(res => {
        const list = res.data.list || []
        this.dbOptions = list.filter(o => o.children && o.children.length);
      })
    },
    onInterfaceChange(id, row) {
      if (this.dataForm.interfaceId === id) return;
      this.fieldList = [];
      if (!id) {
        this.dataForm.interfaceId = '';
        this.dataForm.interfaceName = '';
        this.parameterJson = [];
        return;
      }
      this.fieldList = row.fieldJson ? JSON.parse(row.fieldJson) : [];
      this.dataForm.interfaceId = id;
      this.dataForm.interfaceName = row.fullName;
      this.parameterJson = row.templateJson ? row.templateJson.map(o => ({ ...o, relationField: '', sourceType: 2 })) : [];
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate()
      })
    },
    handleSourceTypeChange(row) {
      row.relationField = row.sourceType == 4 ? this.jnpf.getSystemVariables(1)[0]?.id : '';
    },
    parameterSource(required) {
      const list = [{ label: '自定义', value: 2 }, { label: '系统变量', value: 4 }, { label: '为空', value: 3 }]
      return required ? list.filter(o => o.value != 3) : list
    }
  }
}
</script>
